package com.sugar.follow.up.module.business.controller.app.userpatient;

import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;
import com.sugar.follow.up.framework.security.core.util.SecurityFrameworkUtils;
import com.sugar.follow.up.module.business.controller.app.userpatient.vo.AppUserPatientDetailRespVO;
import com.sugar.follow.up.module.business.controller.app.userpatient.vo.AppUserPatientSaveReqVO;
import com.sugar.follow.up.module.business.controller.app.userpatient.vo.AppUserPatientSimpleRespVO;
import com.sugar.follow.up.module.business.dal.dataobject.userpatient.UserPatientDO;
import com.sugar.follow.up.module.business.service.userpatient.UserPatientService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.sugar.follow.up.framework.common.pojo.CommonResult.successApp;

@Tag(name = "APP - 就诊人")
@RestController
@RequestMapping("/biz/user-patient")
@Validated
public class AppUserPatientController {

    @Resource
    private UserPatientService userPatientService;

    @PutMapping("/create")
    @Operation(summary = "创建就诊人")
    public CommonResult<Boolean> createUserPatient(@Valid @RequestBody AppUserPatientSaveReqVO updateReqVO) {
        userPatientService.appCreateUserPatient(updateReqVO, SecurityFrameworkUtils.getLoginUserId());
        return successApp(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新就诊人")
    public CommonResult<Boolean> updateUserPatient(@Valid @RequestBody AppUserPatientSaveReqVO updateReqVO) {
        userPatientService.appUpdateUserPatient(updateReqVO, SecurityFrameworkUtils.getLoginUserId());
        return successApp(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除就诊人")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:user-patient:delete')")
    public CommonResult<Boolean> deleteUserPatient(@RequestParam("id") Long id) {
        userPatientService.appDeleteUserPatient(id, SecurityFrameworkUtils.getLoginUserId());
        return successApp(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得就诊人详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppUserPatientDetailRespVO> getUserPatient(@RequestParam("id") Long id) {
        UserPatientDO userPatient = userPatientService.getUserPatient(id);
        return successApp(BeanUtils.toBean(userPatient, AppUserPatientDetailRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得所有就诊人")
    public CommonResult<List<AppUserPatientSimpleRespVO>> getUserPatientPage() {
        return successApp(userPatientService.getUserPatientAllList(SecurityFrameworkUtils.getLoginUserId()));
    }
}